<?php
/**
 *ContentPage Controller
 *handels pages edited with the tiny_mce editor
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class ContentPage_Controller
 *Displays pages by pageID
 *@package controller
 */
class ContentPage_Controller extends Controller{
    /**
     * Holds id of page to be displayed
     * @var integer
     */
    private $iPageID = '';

    /**
     * Language in which to show page
     * @var integer
     */
    private $iLang = '';

    //Declare models to retriev page data and views to display the data

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, its shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;

    /**
     * Constructor sets up {@link $oContentView, @link $oNavigationView, @link $oContentModel, @link $oNavigationModel, @link $oLeftNavigationModel, @link $iLang}
     */
    public function __construct(){
        //Gets Viewmanger reference from parent constructor
        parent::__construct();

        //Loads models and views
        $this->oContentModel = Loader::loadModel('contentPage');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oContentView = $this->oViewmanager->getView('contentPage');
        $this->oNavigationView = $this->oViewmanager->getView('navigation');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Checks language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = DEFAULT_LANGUAGE;
        }
    }

    /**
     * Checks GET-Parameters forwarded from Router::route
     * @param Array $aArgs Arguments to check
     */
    public function checkArgs($aArgs){
        //Controller assumes that page id to load is next GET-Element
        $this->iPageID = array_shift($aArgs);

        //Take default startpage from config if no page id was specified
        //This is called when user opens page for first time
        if ($this->iPageID == ''){
            $this->iPageID = DEFAULT_STARTPAGE;
        }

    }

    /**
     * Handles request for content pages
     * Fetchs data and displays it
     * @param Array $aArgs GET-Parameters forwarded from Router:route
     */
    public function execute($aArgs){
        //Determines which page to load
        $this->checkArgs($aArgs);
        //get page data form database and forward it to views
        //ContentPageView needs PageTitel for inputbox value
        $this->oContentView->assign('title', $this->oContentModel->getPageTitle($this->iPageID, $this->iLang));
        $this->oContentView->assign('content', $this->oContentModel->getPageContent($this->iPageID, $this->iLang));
        $this->oNavigationView->assign('contentANDproductPages', $this->oNavigationModel->getPageStructure('NULL', $this->iLang, "(type = 'contentPage' OR type = 'product')"));
        $this->oNavigationView->assign('products', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'product'));
        $this->oNavigationView->assign('categories', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'category'));
        $this->oLeftNavigationView->assign('leftnavigation', $this->oNavigationModel->getPageStructure('NULL', $this->iLang));

        //Inserts data in the correct sections of the website layout
        $this->oViewmanager->setSection('Title', $this->oContentModel->getPageTitle($this->iPageID, $this->iLang));
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }
}
?>